package org.example;

public class Test1 {
    //leetcode 279 完全平方数
    public int coinChange(int[] coins, int amount) {
        //dp[i][j]表示从前i个硬币中选择装入背包使得，总金额数凑成j的最少硬币数
        int n = coins.length;
        int[][] dp = new int[n+1][amount+1];
        for (int i = 1; i <= n; i++) dp[i][0] = (int)2e31-1;
        for (int i = 1; i <= amount; i++) dp[0][i] = (int)2e31-1;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= amount; j++) {
                dp[i][j] = dp[i-1][j];
                if (j >= coins[i-1]) {
                    dp[i][j] = Math.min(dp[i][j],dp[i][j-coins[i-1]] + 1);
                }
            }
        }
        return dp[n][amount] == (int)2e31 - 1 ? -1 : dp[n][amount];
    }

    //优化
    public int coinChange1(int[] coins, int amount) {
        //dp[i][j]表示从前i个硬币中选择装入背包使得，总金额数凑成j的最少硬币数
        int n = coins.length;
        int[] dp = new int[amount+1];
        for (int i = 1; i <= amount; i++) dp[i] = (int)2e31-1;
        for (int i = 1; i <= n; i++) {
            for (int j = coins[i-1]; j <= amount; j++) {
                dp[j] = Math.min(dp[j],dp[j-coins[i-1]] + 1);
            }
        }
        return dp[amount] == (int)2e31 - 1 ? -1 : dp[amount];
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        int[] arr = {2};
        test1.coinChange(arr,3);
        System.out.println(666);
        System.out.println(Integer.MAX_VALUE);
        System.out.println((int)2e31);
    }
}
